package com.lemon.exam.service.impl;

import com.lemon.exam.common.crud.BaseServiceImpl;
import com.lemon.exam.common.exception.CustomException;
import com.lemon.exam.common.util.SecurityUtil;
import com.lemon.exam.entity.po.JobPO;
import com.lemon.exam.repository.JobRepository;
import com.lemon.exam.service.IJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

/**
 * 职位表 服务实现类
 *
 * @author Lemon
 * @since 2025/04/20 22:06:24
 */
@Slf4j
@Service
public class JobServiceImpl extends BaseServiceImpl<JobPO, Long, JobRepository> implements IJobService {
    @Override
    public Mono<String> insert(JobPO param) {
        return Mono.zip(
                super.exist("name", param.getName()),
                SecurityUtil.getUsername()
        ).flatMap(tuple -> {
            if (tuple.getT1()) {
                return Mono.error(new CustomException(402, "职位名称已存在！"));
            } else {
                param.setCreateBy(tuple.getT2());
                return super.insert(param);
            }
        });
    }

    @Override
    public Mono<String> update(JobPO param) {
        return Mono.zip(
                super.exist("name", param.getName(), param.getId()),
                SecurityUtil.getUsername()
        ).flatMap(tuple -> {
            if (tuple.getT1()) {
                return Mono.error(new CustomException(402, "职位名称已存在！"));
            } else {
                param.setUpdateBy(tuple.getT2());
                return super.update(param);
            }
        });
    }
}
